package com.overseasShopping.Action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.overseasShopping.Biz.DetailUserBiz;
import com.overseasShopping.Biz.GoodsBiz;
import com.overseasShopping.Biz.OrderListBiz;
import com.overseasShopping.pojo.DetailUser;
import com.overseasShopping.pojo.OrderList;

public class HandleAction {
	private String orderId;
	private float price;
	private String xinyu;
	private GoodsBiz goodsBiz;
	
	public GoodsBiz getGoodsBiz() {
		return goodsBiz;
	}
	public void setGoodsBiz(GoodsBiz goodsBiz) {
		this.goodsBiz = goodsBiz;
	}
	public String getXinyu() {
		return xinyu;
	}
	public void setXinyu(String xinyu) {
		this.xinyu = xinyu;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public void setState(int state) {
	}

	private OrderListBiz orderListBiz;
	private DetailUserBiz detailUserBiz;
	
	public DetailUserBiz getDetailUserBiz() {
		return detailUserBiz;
	}
	public void setDetailUserBiz(DetailUserBiz detailUserBiz) {
		this.detailUserBiz = detailUserBiz;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public OrderListBiz getOrderListBiz() {
		return orderListBiz;
	}
	public void setOrderListBiz(OrderListBiz orderListBiz) {
		this.orderListBiz = orderListBiz;
	}

	public void setSaleUserId(String saleUserId) {
	}
	

	public String fukuan(){
		OrderList order = orderListBiz.getOrder(orderId);
		order.setState(2);
		orderListBiz.update(order);
		return "success";
	}
	public String buyfukuan(){
		OrderList order = orderListBiz.getOrder(orderId);
		order.setState(2);
		orderListBiz.update(order);
		return "buyfk";
	}
	public String delete(){
		OrderList order = orderListBiz.getOrder(orderId);
		orderListBiz.delete(order);
		return "success";
	}
	public String fahuo(){
		OrderList order = orderListBiz.getOrder(orderId);
		order.setState(3);
		orderListBiz.update(order);
		return "success";
	}
	public String salefahuo(){
		OrderList order = orderListBiz.getOrder(orderId);
		order.setState(3);
		orderListBiz.update(order);
		return "salefh";
	}
	public String queren(){
		OrderList order = orderListBiz.getOrder(orderId);
		order.setState(4);
		goodsBiz.fixGoodState(order.getGoods().getGoodId(), 2);
		orderListBiz.update(order);
		return "success";
	}
	public String buyqueren(){
		OrderList order = orderListBiz.getOrder(orderId);
		order.setState(4);
		goodsBiz.fixGoodState(order.getGoods().getGoodId(), 2);
		orderListBiz.update(order);
		return "buyqr";
	}
	public String pingjia(){
		
		OrderList orderLists = orderListBiz.getOrder(orderId);
		ActionContext.getContext().getApplication().put("orderLists", orderLists);
		String userId = orderLists.getSaleUser().getUserId();
		DetailUser detailUser = detailUserBiz.getUser(userId);
		ActionContext.getContext().getApplication().put("detailUser",detailUser);
			try {
				String realpath = ServletActionContext.getServletContext()
				.getRealPath("/");
				String path = realpath + "uImg/" + detailUser.getUserId() + ".jpg";
	
				File file = new File(path);
				OutputStream out = new FileOutputStream(file);
				InputStream img = detailUser.getUimage().getBinaryStream();
				byte[] b = new byte[img.available()];
				img.read(b);
				out.write(b);
				out.flush();
				out.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		return "pingjia";
	}
	public String salepingjia(){
		
		OrderList orderLists = orderListBiz.getOrder(orderId);
		ActionContext.getContext().getApplication().put("orderLists", orderLists);
		String userId = orderLists.getBuyUser().getUserId();
		DetailUser detailUser = detailUserBiz.getUser(userId);
		ActionContext.getContext().getApplication().put("detailUser", detailUser);
		try {
			String realpath = ServletActionContext.getServletContext()
					.getRealPath("/");
			String path = realpath + "uImg/" + detailUser.getUserId() + ".jpg";
			
			File file = new File(path);
			OutputStream out = new FileOutputStream(file);
			InputStream img = detailUser.getUimage().getBinaryStream();
			byte[] b = new byte[img.available()];
			img.read(b);
			out.write(b);
			out.flush();
			out.close();
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return "salepingjia";
	}
	public String buypingj(){
		OrderList order = orderListBiz.getOrder(orderId);
		String userId = order.getSaleUser().getUserId();
		DetailUser detailUser = detailUserBiz.getUser(userId);
		int state = order.getState();
		if (state==4) {
			order.setState(5);
		}else{
			order.setState(7);
		}
		orderListBiz.update(order);
		int credit = Integer.parseInt(detailUser.getCredit());
		System.out.println(credit+"000000000000000000000000000000000");
		if (xinyu.equals("1")) {
			detailUser.setCredit(String.valueOf(credit+1));
			detailUserBiz.update(detailUser);
		}else {
			detailUser.setCredit(String.valueOf(credit-1));
			detailUserBiz.update(detailUser);
		}
		return "success";
	}
	public String salepingj(){
		OrderList order = orderListBiz.getOrder(orderId);
		String userId = order.getBuyUser().getUserId();
		DetailUser detailUser = detailUserBiz.getUser(userId);
		int state = order.getState();
		if (state==4) {
			order.setState(6);
		}else{
			order.setState(7);
		}
		orderListBiz.update(order);
		int credit = Integer.parseInt(detailUser.getCredit());
		if (xinyu.equals("1")) {
			detailUser.setCredit(String.valueOf(credit+1));
			detailUserBiz.update(detailUser);
		}else {
			detailUser.setCredit(String.valueOf(credit-1));
			detailUserBiz.update(detailUser);
		}
		return "success";
	}
	public String gengjia(){
		OrderList order = orderListBiz.getOrder(orderId);
		order.setPrice(price);
		orderListBiz.update(order);
		return "success";
	}
	public String salegengjia(){
		OrderList order = orderListBiz.getOrder(orderId);
		order.setPrice(price);
		orderListBiz.update(order);
		return "salegg";
	}
	public String xiangq(){
		OrderList orderLists = orderListBiz.getOrder(orderId);
		ActionContext.getContext().getApplication().put("orderLists", orderLists);
		String userId = orderLists.getBuyUser().getUserId();
		DetailUser detailUser = detailUserBiz.getUser(userId);
		ActionContext.getContext().getApplication().put("detailUser", detailUser);
		return "xiangq";
	}
}
